<?php
namespace Admin\Controller;
use Admin\Common\BaseController;
class UserController extends BaseController {
    public function index(){
        $user = M('user');
    	$search = I('get.search');
    	
    	if(!empty($search)){
    		$where = "name like '%".$search."%' AND rank <> 5";
    	}else{
    		$where = "rank <> 5";
    	}

    	$count = $user -> where($where) -> count();
    	$page = new \Think\Page($count,5);
    	$show = $page -> show();
    	$limit = $page -> firstRow.','.$page -> listRows;
    	$data = $user -> limit($limit) -> where($where) -> select();
    	
    	$this -> pagestr = $show;
        $this -> assign('search',$search);
    	$this -> assign('data',$data);
        $this -> display();
    }

    public function add(){
    	$this -> display();
    }

    public function insert(){
    	$user = M('user');

        //处理用户名和密码
        $pass = I('post.pass');
        $name = I('post.name');
        $_POST['pass'] = md5(trim($pass));
        $_POST['name'] = htmlspecialchars(trim($name));
        //判断用户名是否存在
        $row = $user -> where(array('name' => $name)) -> find();
        if($row)
        {
            $this -> error('用户名已存在，请您更换!','',2);
        }
        //添加创建时间
        $addtime = I('post.addtime');
        $_POST['addtime'] = time();

    	$user -> create();

    	$res = $user -> add();

    	if($res){
    		echo "<script>
				alert('添加成功');
				window.location.href='".U('Admin/User/index')."';
				</script>";
    	}else{
    		echo "<script>
				alert('添加失败,请重新添加');
				window.location.href='".U('Admin/User/add')."';
				</script>";
    	}

    }

    public function edit(){
    	$id = I('get.id');
    	$user = M('user');
    	$data = $user -> where('id = "'.$id.'"') -> find();

    	$this -> assign('data',$data);
    	$this -> display();
    }

    public function update(){
    	$user = M('user');
    	$id = (int)I('post.id');
        //处理用户名和密码
        $pass = I('post.pass');
        $name = I('post.name');
        $_POST['pass'] = md5(trim($pass));
        $_POST['name'] = htmlspecialchars(trim($name));
        
        //添加创建时间
        $addtime = I('post.addtime');
        $_POST['addtime'] = time();
    	$user -> create();

    	$res = $user -> save();
    	if($res){
    		echo "<script>
					alert('用户修改成功');
					window.location.href='".U('Admin/User/index')."';
			</script>";
    	}else{
    		echo "<script>
					alert('用户修改失败');
					window.location.href='".U('Admin/User/edit',array('id'=>$id))."';
			</script>";
    	}
    }

    public function alter(){
        $id = I('get.id');
        $user = M('user');
        $data = $user -> where('id = "'.$id.'"') -> find();

        $this -> assign('data',$data);
        $this -> display();
    }

    public function delete(){
    	$id = (int)I('post.id');
    	$user = M('user');
        $ad = $user -> where('id = "'.$id.'"') -> find();
        if($ad['rank'] == 5)
        {
            echo 5;
            return;
        }
    	$res = $user -> where('id = "'.$id.'"') -> delete();
    	if($res){
    		echo 1;
    	}else{
    		echo 2;
    	}
    }

    public function del(){
    	$user = M('user');

    	$id = I('post.id');
    	$ad = $user -> where($where) ->field('rank')-> select();
        foreach($ad as $k => $v)
        {
            foreach ($v as $a => $l) {
            
                if($l == 5)
                {
                    echo "<script>
                        alert('超级管理员不可删除');
                        window.location.href='".U('Admin/User/index')."';
                        </script>";
                        die;
                }
            }
        }
    	//转为字符串
    	$did = implode(',',$id);
    	//in 方法
    	$where['id'] = array('in',$did);

    	$res = $user -> where($where) -> delete();
    	if($res){
    		echo "<script>
				alert('删除成功');
				window.location.href='".U('Admin/User/index')."';
    		</script>";
    	}else{
    		echo "<script>
				alert('删除失败');
				window.location.href='".U('Admin/User/index')."';
    		</script>";
    	}
    }
}